package com.yicheng.statistics.actor.analysis

import akka.actor.Actor
import akka.actor.ActorLogging
import akka.actor.Props
import com.yicheng.statistics.actor.SaveHtaData
import akka.actor.ActorRef
import com.yicheng.statistics.common.TimeUtils
import com.yicheng.statistics.repo.cassandra.DataInfoDB
import scala.util.Success
import scala.util.Failure
import java.util.Date
import org.joda.time.DateTime
import com.yicheng.statistics.repo.RTAModel.VehicleMileage
import com.yicheng.statistics.service.HTAService
import com.yicheng.statistics.actor.AnalysisParser
import akka.actor.SupervisorStrategy.Stop

/**
 * 里程统计分析
 */
class Mileage extends Actor with ActorLogging with AnalysisParser{
  
  val saveHtaDataActor:ActorRef = context.child("saveHtaData").
    getOrElse(context.actorOf(Props[SaveHtaData], "saveHtaData"))
  
  import scala.concurrent.ExecutionContext.Implicits.global
  
  def receive: Receive = {
    case (deviceId:String,deviceType:Int) =>
      log.info("里程统计 deviceId "+deviceId)
      
      val mileage = MileageData(None, None, None, None, None, None, None, None,None,None, deviceId, deviceType)//(startmil, endmil) 开始里程(第一条) 结束里程(最后一条)
      
      parseOneDayDataInfo(deviceType, deviceId, mileage, Parser _)
    case Stop =>
      //context.parent ! Stop
      log.info("mileage stop")
      context.actorSelection("/user/daemon") ! MileageStop
    case _ => log.info("recive data : ")
  }
  
  def Parser( t:MileageData, result:Seq[com.yicheng.statistics.repo.model.Data.DataInfo], isEnd:Boolean) : MileageData = {
    var mileage = t
    result.map( di => {
      log.info("get datainfo "+di)
      val data = di.data_vehicle.overallData.get//.get.overallData.get
      val pos = di.data_vehicle.posData.get
      
      if(t.startTime.isEmpty) {//第一条数据设置开始
        mileage = mileage.copy(
                   start = Some(data.currentMileage), 
                   startLat = Some(pos.getLatitude),
                   startLon = Some(pos.getLongitude),
                   startTime = Some(di.data_time),
                   socStatus = Some(data.socStatus),
                   totalSoc = Some(0))
      }else{
        if(data.socStatus > mileage.socStatus.getOrElse(0)) {//充电了后面的电量比前面的大
          mileage = mileage.copy(
                   end = Some(data.currentMileage), 
                   endLat = Some(pos.getLatitude),
                   endLon = Some(pos.getLongitude),
                   endTime = Some(di.data_time),
                   socStatus = Some(data.socStatus),
                   totalSoc = mileage.totalSoc
                   )
        }else{
          val total = mileage.totalSoc.getOrElse(0) + (mileage.socStatus.getOrElse(0) - data.socStatus)
          mileage = mileage.copy(
                   end = Some(data.currentMileage), 
                   endLat = Some(pos.getLatitude),
                   endLon = Some(pos.getLongitude),
                   endTime = Some(di.data_time),
                   socStatus = Some(data.socStatus),
                   totalSoc = Some(total)
                   )
        }
        
      }
    })
    
    if( isEnd && !mileage.start.isEmpty){
      val vehicleMileage = VehicleMileage(mileage.deviceType,
                                        mileage.deviceId,
                                        mileage.start,
                                        mileage.end,
                                        mileage.startTime.get,
                                        mileage.endTime.get,
                                        DateTime.now().toDate(),
                                        DateTime.now().toDate(),
                                        None,
                                        mileage.startLon,
                                        mileage.startLat,
                                        mileage.endLon,
                                        mileage.endLat,
                                        Some(mileage.totalSoc.get.toString())
                                        )
      log.info("mileage process end result:"+vehicleMileage)
      saveHtaDataActor ! vehicleMileage//保存数据
    }
    log.info("one process end mileage is "+mileage)
    mileage
  }
}

case class MileageData(
    start:Option[Float], //开始里程
    startLat:Option[Double],
    startLon:Option[Double],
    startTime:Option[Date],
    end:Option[Float],//结束里程
    endLat:Option[Double],
    endLon:Option[Double],
    endTime:Option[Date],
    socStatus:Option[Int],//电池电量百分比
    totalSoc:Option[Int],
    deviceId:String,
    deviceType:Int
    )
case class MileageStop()